package com.common.ui.recycleview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.common.R;
import com.common.ui.recycleview.adapter.RecyclerviewBasicPageAdapter;
import com.common.ui.recycleview.adapter.RecyclerviewBasicPageAdapterTwo;
import com.common.utils.NetworkUtils;

/**
 * Created by jack
 */

public class PullLoadMoreRecyclerView<T> extends LinearLayout {

    private Context context;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView.LayoutManager layoutManager;
    private boolean isRefreshing = false;//是否正在刷新(默认打开时就开始加载)
    private boolean isPushRefreshing = false;//是否正在加载更多
    private int footerState = 1;//1正在加载... 2加载失败,点击重新加载 3没有更多数据了!
    public PullLoadMoreListener pullLoadMoreListener;
    public boolean isShowFooter = false;//是否显示底部加载更多View
    private boolean isShowHeader = false;//是否显示头部View
    public boolean pullRefreshEnable = true;//是否可以下拉刷新
    public boolean pushRefreshEnable = true;//是否可以上拉加载更多
    private RecyclerviewBasicPageAdapter adapter = null;
    private RecyclerviewBasicPageAdapterTwo adapterTwo = null;

    public PullLoadMoreRecyclerView(Context context) {
        this(context, null);
    }

    public PullLoadMoreRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PullLoadMoreRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
    }

    public void init(RecyclerView recyclerView, SwipeRefreshLayout swipeRefreshLayout) {

        this.recyclerView = recyclerView;
        this.swipeRefreshLayout = swipeRefreshLayout;
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.colorPrimary, R.color.colorPrimary);
        recyclerView.setLayoutManager(new LinearLayoutManager(context,
                LinearLayoutManager.VERTICAL, false));//默认设置线性布局管理器
        recyclerView.setHasFixedSize(true);//提高性能
        recyclerView.setItemAnimator(new DefaultItemAnimator());//设置效果
        recyclerView.addOnScrollListener(new RecyclerViewOnScroll(this));//设置底部刷新
        recyclerView.setOnTouchListener(new onTouchRecyclerView());

        //注释了局部item更新动画 并且再设置默认局部更新动画false可以结局局部更新闪烁问题  默认的动画就是闪烁效果
//        recyclerView.setItemAnimator(new SlideInLeftAnimator());
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (isRefreshing) {
                    return;
                } else {
                    isRefreshing = true;
                    isPushRefreshing = false;
                    footerState = 1;
                    isShowFooter = false;
                    isShowHeader = false;
                    if (pullLoadMoreListener != null) {
                        if (adapter != null) {
                            adapter.reset();
                            adapter.notifyDataSetChanged();
                        }
                        if (adapterTwo != null) {
                            adapterTwo.reset();
                            adapterTwo.notifyDataSetChanged();
                        }
                        pullLoadMoreListener.onRefresh();
                    }
                }
            }
        });
    }

    public void setSwipeRefreshLayoutCircleColor(int color){
        swipeRefreshLayout.setColorSchemeResources(color, color,color);
    }

    public class onTouchRecyclerView implements OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return false;
        }
    }

    public interface PullLoadMoreListener {
        void onRefresh();

        void onLoadMore();
    }

    //==============================get set ===============================
    public void setLayoutManager(RecyclerView.LayoutManager layoutManager) {
        this.layoutManager = layoutManager;
        recyclerView.setLayoutManager(layoutManager);
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    public void loadMore() {
        if (pullLoadMoreListener != null) {
            if (isShowFooter) {
                if (footerState == 1) {
                    if (isPushRefreshing()) {
                        setPullRefreshEnable(false);
                        return;
                    } else {
                        if (NetworkUtils.getInstance().isNetworkConnected()) {
                            setPullRefreshEnable(false);
                            if (adapter != null) {
                                adapter.setLoadMore();
                            }
                            if (adapterTwo != null) {
                                adapterTwo.setLoadMore();
                            }
                            pullLoadMoreListener.onLoadMore();
                        } else {
                            setPullRefreshEnable(true);
                            footerState = 2;
                            isPushRefreshing = false;
                            if (adapter != null) {
                                this.adapter.setFooterState(2);
                                this.adapter.setShowFooter(true);
                                this.adapter.notifyDataSetChanged();
                            }
                            if (adapterTwo != null) {
                                this.adapterTwo.setFooterState(2);
                                this.adapterTwo.setShowFooter(true);
                                this.adapterTwo.notifyDataSetChanged();
                            }
                        }
                    }
                } else if (footerState == 2) {
                    setPullRefreshEnable(true);
                    return;
                }
            } else {
                isShowFooter = true;
                if (NetworkUtils.getInstance().isNetworkConnected()) {
                    setPullRefreshEnable(false);
                    footerState = 1;
                    isPushRefreshing = true;
                    if (adapter != null) {
                        this.adapter.setLoadMore();
                        this.adapter.setFooterState(1);
                        this.adapter.setShowFooter(true);
                        this.adapter.notifyDataSetChanged();
                    }
                    if (adapterTwo != null) {
                        this.adapterTwo.setLoadMore();
                        this.adapterTwo.setFooterState(1);
                        this.adapterTwo.setShowFooter(true);
                        this.adapterTwo.notifyDataSetChanged();
                    }
                    pullLoadMoreListener.onLoadMore();
                } else {
                    setPullRefreshEnable(true);
                    footerState = 2;
                    isPushRefreshing = false;
                    if (adapter != null) {
                        this.adapter.setFooterState(2);
                        this.adapter.setShowFooter(true);
                        this.adapter.notifyDataSetChanged();
                    }
                    if (adapterTwo != null) {
                        this.adapterTwo.setFooterState(2);
                        this.adapterTwo.setShowFooter(true);
                        this.adapterTwo.notifyDataSetChanged();
                    }
                }
            }
        }
    }

    /**
     * 重置
     */
    public void reset() {
        isRefreshing = false;
        isPushRefreshing = false;
        footerState = 1;
        isShowFooter = false;
        isShowHeader = false;
    }

    public void setNoMoreData() {
        footerState = 3;
        isPushRefreshing = false;
        setPullRefreshEnable(true);
    }

    /**
     * 上拉加载更多之后
     */
    public void setMoreData() {
        isShowFooter = false;
        footerState = 1;
        isPushRefreshing = false;
        setPullRefreshEnable(true);
    }

    public void refreshFinish() {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
        isRefreshing = false;
    }

    public void setOnPullLoadMoreListener(PullLoadMoreListener listener) {
        this.pullLoadMoreListener = listener;
    }

    public void setAdapter(RecyclerviewBasicPageAdapter adapter) {
        this.adapter = adapter;
        recyclerView.setAdapter(this.adapter);
    }

    public void setAdapterTwo(RecyclerviewBasicPageAdapterTwo adapter) {
        this.adapterTwo = adapter;
        recyclerView.setAdapter(this.adapterTwo);
    }

    public int getFooterState() {
        return footerState;
    }

    public void setFooterState(int footerState) {
        this.footerState = footerState;
    }

    public boolean isPullRefreshEnable() {
        return pullRefreshEnable;
    }

    public void setPullRefreshEnable(boolean pullRefreshEnable) {
        this.pullRefreshEnable = pullRefreshEnable;
        if (pullRefreshEnable) {
            swipeRefreshLayout.setEnabled(true);
        } else {
            swipeRefreshLayout.setEnabled(false);
        }
    }

    public boolean isPushRefreshEnable() {
        return pushRefreshEnable;
    }

    public void setPushRefreshEnable(boolean pushRefreshEnable) {
        this.pushRefreshEnable = pushRefreshEnable;
    }

    public boolean isRefreshing() {
        return isRefreshing;
    }

    public void setRefreshing(boolean refreshing) {
        isRefreshing = refreshing;
    }

    public boolean isPushRefreshing() {
        return isPushRefreshing;
    }

    public void setPushRefreshing(boolean pushRefreshing) {
        isPushRefreshing = pushRefreshing;
    }

    public PullLoadMoreListener getPullLoadMoreListener() {
        return pullLoadMoreListener;
    }

    public void setPullLoadMoreListener(PullLoadMoreListener pullLoadMoreListener) {
        this.pullLoadMoreListener = pullLoadMoreListener;
    }

    public boolean isShowFooter() {
        return isShowFooter;
    }

    public void setShowFooter(boolean showFooter) {
        isShowFooter = showFooter;
    }
}
